package com.seata.core.utils;

import com.seata.core.enums.HttpCodeEnum;

/**
 * @author 蚂蚁会花呗
 * @date 2021/6/3 16:38
 */
public class Response {

    private int code = 200;

    private String msg;

    private Object data;

    /**
     * 无参构造函数
     */
    public Response() {

    }

    /**
     * 自定义状态码和消息
     * @param code
     * @param msg
     */
    public Response(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }


    /***
     * 自定义返回参数
     * @param code
     * @param msg
     * @param data
     */
    public Response(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功，不带返回参数
     * @return
     */
    public static Response ok(){
        return new Response(HttpCodeEnum.OK.getCode(),"成功！");
    }

    /**
     * 成功，自定义提示信息
     * @param msg
     * @return
     */
    public static Response ok(String msg){
        return new Response(HttpCodeEnum.OK.getCode(),msg);
    }

    /**
     * 成功，携带返回参数
     * @param data
     * @return
     */
    public static Response okData(Object data){
        return new Response(HttpCodeEnum.OK.getCode(),"成功！").setData(data);
    }

    /**
     *
     * @param msg 自定义提示信息
     * @param data 参数
     * @return
     */
    public static Response okData(String msg,Object data){
        return new Response(HttpCodeEnum.OK.getCode(),msg).setData(data);
    }

    /**
     * 失败，不带返回参数
     * @return
     */
    public static Response fail(){
        return new Response(HttpCodeEnum.SERVER_ERROR.getCode(),"失败！");
    }

    /**
     * 失败，自定义返回消息
     * @param msg
     * @return
     */
    public static Response fail(String msg){
        return new Response(HttpCodeEnum.SERVER_ERROR.getCode(),msg);
    }

    /**
     * 失败，返回参数
     * @param data
     * @return
     */
    public static Response failData(Object data){
        return new Response(HttpCodeEnum.SERVER_ERROR.getCode(),"失败！").setData(data);
    }

    /**
     * 失败，自定义消息，并返回数据
     * @param msg
     * @param data
     * @return
     */
    public static Response failData(String msg,Object data){
        return new Response(HttpCodeEnum.SERVER_ERROR.getCode(),msg).setData(data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public Response setData(Object data) {
        this.data = data;
        return this;
    }
}
